//
//  VisaServiceViewController.m
//  MyTravel
//
//  Created by Hunk on 11-6-11.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "VisaServiceViewController.h"
#import "VisaNeedDetailViewController.h"
#import "NetworkManager.h"
#import "NSString+URLEncoding.h"
#import "MyLoadingView.h"

#define KEY_NAME   @"name"

#define KEY_MOBILE @"mobile"
#define KEY_WORK   @"workPhone"
#define KEY_QQ     @"qq"
#define KEY_FAX    @"fax"
#define KEY_EMAIL  @"email"

#define MESSAGEBODY @"我有以下问题想要资询:<点击此处写入内容>\n"

#define TAG_TABLEVIEW (21)

@interface VisaServiceViewController (Private)

-(void)addLabelOnCell:(UITableViewCell*)cell
                 text:(NSString*)text
                frame:(CGRect)frame 
              bgColor:(UIColor*)bgColor 
            textColor:(UIColor*)textColor 
           fontOfSize:(UIFont*)fontOfSize 
        lineBreakMode:(UILineBreakMode)lineBreakMode 
        textAlignment:(UITextAlignment)textAlignment;

- (void)displayComposerSheet:(NSString*)emailAddress;


- (void)launchMailAppOnDevice:(NSString*)emailAddress;

@end

@implementation VisaServiceViewController
@synthesize countryId, visaType, visaAddress;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)dealloc
{
    SAFE_RELEASE(segmentedControl)
    SAFE_RELEASE(titleArray)
    SAFE_RELEASE(visaInfoDictionary)
    SAFE_RELEASE(visaBaseInfoArray)
    SAFE_RELEASE(visaNoticeArray)
    SAFE_RELEASE(visaNeedArray)
    SAFE_RELEASE(visaContactInfoArray)
    
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle
// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];
    
    // Init data
    visaBaseInfoArray = [[NSMutableArray alloc] init];
    visaNoticeArray = [[NSMutableArray alloc] init];
    visaNeedArray = [[NSMutableArray alloc] init];
    visaInfoDictionary = [[NSMutableDictionary alloc] init];
    visaContactInfoArray = [[NSMutableArray alloc] init];
    
    titleArray = [[NSArray alloc] initWithObjects:
                  @"有效时间:",
                  @"停留时间:", 
                  @"办理周期:",
                  @"是否面试:",
                  @"适用范围:",
                  @"办理费用:",
                  @"办证地点:", nil];
    
    // Add tableView
    UITableView* tableView = [[[UITableView alloc] initWithFrame:CGRectMake(0, 32, 320, 420-32)
                                                          style:UITableViewStyleGrouped] autorelease];
    [tableView setBackgroundColor:[UIColor groupTableViewBackgroundColor]];
    [tableView setDelegate:self];
    [tableView setDataSource:self];
    [tableView setTag:TAG_TABLEVIEW];
    [self.view addSubview:tableView];
    
    // toolbar
    UIToolbar* toolbar = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, 320, 40)] autorelease];
    [toolbar setBackgroundColor:[UIColor clearColor]];
    [toolbar setBarStyle:UIBarStyleDefault];
    [toolbar setTintColor:TINTCOLOR];
    [self.view addSubview:toolbar];
    
    // SegmentedControl
    segmentedControl = [[UISegmentedControl alloc] initWithItems:
                                        [NSArray arrayWithObjects:@"基本信息", @"注意事项", @"所需材料", @"办理签证", nil]];
    [segmentedControl setFrame:CGRectMake(10, 5, 300, 30)];
    [segmentedControl setSegmentedControlStyle:UISegmentedControlStyleBar];
    [segmentedControl addTarget:self 
                         action:@selector(segmentedControlHandle:) 
               forControlEvents:UIControlEventValueChanged];
    [segmentedControl setTintColor:TINTCOLOR];
    [segmentedControl setSelectedSegmentIndex:0];
    [toolbar addSubview:segmentedControl];
    
    // Loading view
    [[MyLoadingView sharedMyLoadingView] startLoadingAnimation];
    
    // NetworkRequest
    int nVisaType = 0;
    if([visaType isEqualToString:@"旅游签证"]) nVisaType = 0;
    else if([visaType isEqualToString:@"商务签证"]) nVisaType = 1;
    else if([visaType isEqualToString:@"探亲访友"]) nVisaType = 2;
    else if([visaType isEqualToString:@"旅游加急"]) nVisaType = 3;
    else 
        nVisaType = 0;
//    if([visaType isEqualToString:@"旅游签证"]) nVisaType = 1;
//    else if([visaType isEqualToString:@"旅游签证-北京"]) nVisaType = 2;
//    else if([visaType isEqualToString:@"旅游签证-上海"]) nVisaType = 3;
//    else if([visaType isEqualToString:@"旅游签证-广州"]) nVisaType = 4;
//    
//    else if([visaType isEqualToString:@"商务签证"]) nVisaType = 5;
//    else if([visaType isEqualToString:@"商务签证-北京"]) nVisaType = 6;
//    else if([visaType isEqualToString:@"商务签证-上海"]) nVisaType = 7;
//    else if([visaType isEqualToString:@"商务签证-广州"]) nVisaType = 8;
//    
//    else if([visaType isEqualToString:@"探亲访友"]) nVisaType = 9;
//    else if([visaType isEqualToString:@"探亲访友-北京"]) nVisaType = 10;
//    else if([visaType isEqualToString:@"探亲访友-上海"]) nVisaType = 11;
//    else if([visaType isEqualToString:@"探亲访友-广州"]) nVisaType = 12;
        
    NSDictionary* parameters = [NSDictionary dictionaryWithObjectsAndKeys:
                                [countryId URLEncodedString], @"countryId", 
                                [NSString stringWithFormat:@"%d", nVisaType], @"visaType", 
                                [visaAddress URLEncodedString], @"visaAddress", nil];
    
    [[NetworkManager sharedNetworkManager] requestServerForMethod:NetworkRequest_GetVisaBaseAndNoticeInfo
                                                   WithParameters:parameters
                                                 AndCallBackScope:self
                                                    AndSuccessSel:@"onSuccess:"
                                                     AndFailedSel:@"onFailed:"];
}

#pragma mark -
#pragma mark UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView 
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section 
{
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
            return [visaBaseInfoArray count];
            break;
        case 1:
            return [visaNoticeArray count];
            break;
        case 2:
            return [visaNeedArray count];
            break;
        case 3:
            return 5;
            break;
        default:
            break;
    }
    return 0;
}

- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 3:
            return [[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_NAME];
            break;
        default:
            break;
    }
    return nil;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if([visaNoticeArray count] || [visaBaseInfoArray count])
    {
        switch (segmentedControl.selectedSegmentIndex)
        {
            case 0:
            {
                CGSize size = [[visaBaseInfoArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:16]                           
                                                                        constrainedToSize:CGSizeMake(200, 500)                                        
                                                                            lineBreakMode:UILineBreakModeCharacterWrap];
                return (size.height > 50.0 ? size.height + 20 : 50.0);
            }
                break;
            case 1:
            {
                CGSize size = [[visaNoticeArray objectAtIndex:indexPath.row] sizeWithFont:[UIFont systemFontOfSize:16]                           
                                                                        constrainedToSize:CGSizeMake(300, 1000)                                        
                                                                            lineBreakMode:UILineBreakModeCharacterWrap];
                return (size.height > 50.0 ? size.height + 20 : 50.0);
            }
                break;
            case 2:
                return 50.0f;
                break;
            case 3:
                return 60.0f;
                break;
            default:
                break;
        }
    }
    return 50.0f;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath 
{ 
    static NSString *CellIdentifier = @"Cell";
    
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:CellIdentifier];
    cell = [[[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault 
                                   reuseIdentifier:CellIdentifier] autorelease];
    
    if([visaNoticeArray count] || [visaBaseInfoArray count])
    {
        switch (segmentedControl.selectedSegmentIndex)
        {
            case 0:
            {
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                // Title
                [self addLabelOnCell:cell
                                text:[titleArray objectAtIndex:indexPath.row]
                               frame:CGRectMake(10, 5, 85, 40) 
                             bgColor:[UIColor clearColor] 
                           textColor:[UIColor blackColor] 
                          fontOfSize:[UIFont boldSystemFontOfSize:18]
                       lineBreakMode:UILineBreakModeCharacterWrap
                       textAlignment:UITextAlignmentLeft];
                
                // Content
                NSString* textString = [visaBaseInfoArray objectAtIndex:indexPath.row];
                if(5 == indexPath.row)
                {
                    textString = [NSString stringWithFormat:@"¥%@", textString];
                }
                
                CGSize size = [textString sizeWithFont:[UIFont systemFontOfSize:16]                           
                                                                        constrainedToSize:CGSizeMake(200, 1000)                                        
                                                                            lineBreakMode:UILineBreakModeCharacterWrap];
                float fHeight = (size.height > 50.0 ? size.height + 20 : 50.0);
                
                [self addLabelOnCell:cell
                                text:textString
                               frame:CGRectMake(90, 2, 190, fHeight)
                             bgColor:[UIColor clearColor]
                           textColor:TEXTCOLOR
                          fontOfSize:[UIFont fontWithName:@"AmericanTypewriter" size:16]
                       lineBreakMode:UILineBreakModeTailTruncation
                       textAlignment:UITextAlignmentRight];
            }
                break;
            case 1:
            {
                cell.selectionStyle = UITableViewCellSelectionStyleNone;
                
                cell.textLabel.font = [UIFont fontWithName:@"AmericanTypewriter" size:16];
                cell.textLabel.lineBreakMode = UILineBreakModeWordWrap;
                cell.textLabel.numberOfLines = 10;
                cell.textLabel.text = [NSString stringWithFormat:@"%d.%@", (indexPath.row + 1), [visaNoticeArray objectAtIndex:indexPath.row]];
            }
                break;
            case 2:
            {
                cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                
                NSArray* array = [NSArray arrayWithObjects:
                                  @"personalId.png",
                                  @"assetsEvidence.png", 
                                  @"employment.png", 
                                  @"studentAndChildren.png", 
                                  @"elderly.png", 
                                  @"other.png", nil];
                
                UIImageView* imageView = [[UIImageView alloc] initWithFrame:CGRectMake(15, 2.5f, 45, 45)];
                [imageView setBackgroundColor:[UIColor clearColor]];
                [imageView setImage:[UIImage imageNamed:[array objectAtIndex:indexPath.row]]];
                [cell.contentView addSubview:imageView];
                [imageView release];
                
                UILabel* label = [[UILabel alloc] initWithFrame:CGRectMake(90, 5, 200, 50)];
                [label setBackgroundColor:[UIColor clearColor]];
                [label setFont:[UIFont boldSystemFontOfSize:18]];
                [label setTextAlignment:UITextAlignmentLeft];
                [label setText:[visaNeedArray objectAtIndex:indexPath.row]];
                [cell.contentView addSubview:label];
                [label release];
                
//                cell.imageView.image = [UIImage imageNamed:];
//                
//                cell.textLabel.text = [visaNeedArray objectAtIndex:indexPath.row];
            }
                break;
            case 3:
            {
                cell.selectionStyle = UITableViewCellSelectionStyleBlue;
                switch (indexPath.row)
                {
                    case 0:
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        break;
                    case 1:
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        break; 
                    case 2:
                        cell.accessoryType = UITableViewCellAccessoryNone;
                        break;
                    case 3:
                        cell.accessoryType = UITableViewCellAccessoryNone;
                        break;
                    case 4:
                        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
                        break;
                    default:
                        break;
                }
                NSDictionary* tempDic = [visaContactInfoArray objectAtIndex:0];
                
                // Keys for contact image and label text
                NSArray* arrayKeys = [NSArray arrayWithObjects:KEY_MOBILE, KEY_WORK, KEY_QQ, KEY_FAX, KEY_EMAIL, nil];
                
                // Contact imageView
                NSString* nameString = [NSString stringWithFormat:@"%@.png", [arrayKeys objectAtIndex:indexPath.row]];
                UIImage* mobileImage = [UIImage imageNamed:nameString];
                UIImageView* mobileImageView = [[UIImageView alloc] initWithImage:mobileImage];
                [mobileImageView setFrame:CGRectMake(10, 10, mobileImage.size.width, mobileImage.size.height)];
                mobileImageView.backgroundColor = [UIColor clearColor];
                [cell.contentView addSubview:mobileImageView];
                [mobileImageView release];
                
                // Contact label
                [self addLabelOnCell:cell
                                text:[tempDic objectForKey:[arrayKeys objectAtIndex:indexPath.row]] 
                               frame:CGRectMake(50, 10, 225, 40)
                             bgColor:[UIColor clearColor] 
                           textColor:TEXTCOLOR
                          fontOfSize:[UIFont fontWithName:@"AmericanTypewriter" size:20]
                       lineBreakMode:UILineBreakModeTailTruncation
                       textAlignment:UITextAlignmentRight];
            }
                break;
            default:
                break;
        }
    }
    return cell;
}

#pragma mark -
#pragma mark UITableViewDelegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
        {}
            break;
        case 2: // 签证所需材料
        {
            VisaNeedDetailViewController* visaNeedDetailViewController = [[VisaNeedDetailViewController alloc] initWithStyle:UITableViewStylePlain];
            visaNeedDetailViewController.countryId = countryId;
            visaNeedDetailViewController.visaType = visaType;
            
            visaNeedDetailViewController.title = [visaNeedArray objectAtIndex:indexPath.row];
            
            switch (indexPath.row)
            {
                case 0: // 个人身份证明
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_PERSONALIDENTIFICATION;
                    //[self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
                }
                    break;
                case 1: // 个人资产证明
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_PROTERTY;
                    //[self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
                }
                    break;
                case 2: // 工作证明
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_EMPLOYMENT;
                    //[self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
                }
                    break;
                case 3: // 学生及儿童
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_STUDENTSCHILDREN;
                    //[self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
                }
                    break;
                case 4: // 老年人
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_ELDERLY;
                    //[self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
                }
                    break;
                case 5: // 其它
                {
                    visaNeedDetailViewController.visaNeedStatus = VISANEEDSTATUS_OTEHRS;
                }
                    break;
                default:
                    break;
            }
            [self.navigationController pushViewController:visaNeedDetailViewController animated:YES];
            [visaNeedDetailViewController release];
        }
            break;
        case 3: // 办理签证
        {
            switch (indexPath.row)
            {
                case 0: // Mobile
                {
                    if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
                    {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", [[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_MOBILE]]]];
                    }
                    else
                    {
                        ALERT_GENERAL(@"貌似您的设备不支持电话功能")
                    }
                }
                    break;
                case 1: // Work
                {
                    if([[UIDevice currentDevice].model isEqualToString:@"iPhone"])
                    {
                        [[UIApplication sharedApplication] openURL:[NSURL URLWithString:[NSString stringWithFormat:@"tel://%@", [[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_WORK]]]];
                    }
                    else
                    {
                        ALERT_GENERAL(@"貌似您的设备不支持电话功能")
                    }
                }
                    break;
                case 4: // Email
                {
                    Class mailClass = (NSClassFromString(@"MFMailComposeViewController"));
                    if(mailClass)
                    {
                        if([mailClass canSendMail])
                            [self displayComposerSheet:[[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_EMAIL]];
                        else
                            [self launchMailAppOnDevice:[[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_EMAIL]];
                    }
                    else
                        [self launchMailAppOnDevice:[[visaContactInfoArray objectAtIndex:0] objectForKey:KEY_EMAIL]];
                }
                    break;
                default:
                    break;
            }
        }
            break;
        default:
            break;
    }
//    [tableView reloadRowsAtIndexPaths:[NSArray arrayWithObject:indexPath] withRowAnimation:UITableViewRowAnimationNone];
}

#pragma mark -
#pragma mark segmentedControlHandle
- (void)segmentedControlHandle:(id)sender
{   
    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
    
    switch (segmentedControl.selectedSegmentIndex)
    {
        case 0:
        {   
            NSDictionary* baseInfoDic = [[visaInfoDictionary objectForKey:@"visaBaseInfo"] objectAtIndex:0];
            
            if([visaBaseInfoArray count])
            {
                [visaBaseInfoArray removeAllObjects];
            }
            
            for(int i = 0; i < [[baseInfoDic allKeys] count]; i++)
            {
                [visaBaseInfoArray addObject:[baseInfoDic objectForKey:[[NSArray arrayWithObjects:
                                                                         @"effectiveCycle", 
                                                                         @"stayCycle", 
                                                                         @"transactCycle", 
                                                                         @"interview", 
                                                                         @"scope",
                                                                         @"visaFees", 
                                                                         @"visaAddress", nil] objectAtIndex:i]]];
            }
            [tableView reloadData];
        }
            break;
        case 1:
        {   
            NSString* tempString = [visaInfoDictionary objectForKey:@"noticeInfo"];
            
            if([tempString length])
            {
                NSArray* tempArray = [tempString componentsSeparatedByString:@"[OVER]"];
                
                if([visaNoticeArray count])
                {
                    [visaNoticeArray removeAllObjects];
                }
                
                for(int i = 0; i < [tempArray count]; i++)
                {
                    [visaNoticeArray addObject:[tempArray objectAtIndex:i]];
                }
                [tableView reloadData];
            }
            else
            {
                ALERT_GENERAL(@"暂无注意事项信息")
                
                [tableView reloadData];
            }
        }
            break;
        case 2:
        {
            [visaNeedArray removeAllObjects];
            [visaNeedArray addObjectsFromArray:[NSArray arrayWithObjects:
                                                @"个人身份证明",
                                                @"资产证明", 
                                                @"工作证明", 
                                                @"学生及儿童", 
                                                @"老年人", 
                                                @"其它", nil]];
            [tableView reloadData];
        }
            break;
        case 3:
        {
            NSArray* array = [visaInfoDictionary objectForKey:@"contactInfo"];
            
            [visaContactInfoArray addObjectsFromArray:array];
            
            [tableView reloadData];
        }
            break;
        default:
            break;
    }
}

#pragma mark - 
#pragma mark NetworkManager callback
- (void)onSuccess:(id)sender
{   
    NSDictionary* resultDic = (NSDictionary*)sender;
    [visaInfoDictionary setDictionary:resultDic];
    
    NSDictionary* baseInfoDic = [[visaInfoDictionary objectForKey:@"visaBaseInfo"] objectAtIndex:0];
    
    if([visaBaseInfoArray count])
    {
        [visaBaseInfoArray removeAllObjects];
    }
    
    for(int i = 0; i < [[baseInfoDic allKeys] count]; i++)
    {
        [visaBaseInfoArray addObject:[baseInfoDic objectForKey:[[NSArray arrayWithObjects:
                                                                 @"effectiveCycle", 
                                                                 @"stayCycle", 
                                                                 @"transactCycle", 
                                                                 @"interview", 
                                                                 @"scope",
                                                                 @"visaFees", 
                                                                 @"visaAddress", nil] objectAtIndex:i]]];
    }
    
    UITableView* tableView = (UITableView*)[self.view viewWithTag:TAG_TABLEVIEW];
    [tableView reloadData];
}

- (void)onFailed:(id)sender
{
    //NSDictionary* resultDic = (NSDictionary*)sender;
    
    UIAlertView* alertView = [[UIAlertView alloc] initWithTitle:@"服务器正在维护中，敬请期待"
                                                        message:nil
                                                       delegate:self
                                              cancelButtonTitle:@"好的"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

#pragma mark -
#pragma mark addLabelOnCell
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    [self.navigationController popViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark Compose Mail
-(void)displayComposerSheet:(NSString*)emailAddress 
{
	MFMailComposeViewController *picker = [[MFMailComposeViewController alloc] init];
	picker.mailComposeDelegate = self;
    picker.navigationBar.tintColor = TINTCOLOR;
	
	[picker setSubject:[NSString stringWithFormat:@"咨询<%@>", self.title]];
	
	// Set up recipients
    [picker setToRecipients:[NSArray arrayWithObjects:emailAddress, nil]];
    
	// Fill out the email body text
	[picker setMessageBody:MESSAGEBODY isHTML:NO];
	
	[self presentModalViewController:picker animated:YES];
    [picker release];
}

#pragma mark -
#pragma mark Workaround
// Launches the Mail application on the device.
-(void)launchMailAppOnDevice:(NSString*)emailAddress 
{	
	// Recipient(s)
	NSString* strRecipient = [NSString stringWithFormat:@"mailto:%@?subject=%@", emailAddress, [NSString stringWithFormat:@"咨询<%@>", self.title]];
	
	// Mail body
	NSString* strBody = [NSString stringWithFormat:@"&body=%@", MESSAGEBODY];
	
	NSString* strEmail = [NSString stringWithFormat:@"%@%@", strRecipient, strBody];
	strEmail = [strEmail stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
	
	[[UIApplication sharedApplication] openURL:[NSURL URLWithString:strEmail]];
}

// Dismisses the email composition interface when users tap Cancel or Send. Proceeds to update the message field with the result of the operation.
- (void)mailComposeController:(MFMailComposeViewController*)controller didFinishWithResult:(MFMailComposeResult)result error:(NSError*)error 
{		
    NSString* strSendEmailresult = nil;
    switch (result)
    {
        case MFMailComposeResultCancelled: 
            strSendEmailresult = @"操作已被取消";
            break;
        case MFMailComposeResultSaved:
            strSendEmailresult = @"邮件已被保存到草稿箱";
            break;
        case MFMailComposeResultSent:
            strSendEmailresult = @"邮件正在发送,请保持网络通畅";
            break;
        case MFMailComposeResultFailed:
            strSendEmailresult = @"邮件发送失败，请重新发送";
            break;
        default:
            break;
    }
    
    //    UIAlertView* alert = [[UIAlertView alloc] initWithTitle:strSendEmailresult 
    //                                                    message:nil 
    //                                                   delegate:nil
    //                                          cancelButtonTitle:@"好的" 
    //                                          otherButtonTitles:nil];
    //    [alert show];
    //    [alert release];
    
	[self dismissModalViewControllerAnimated:YES];
}

#pragma mark -
#pragma mark addLabelOnCell
-(void)addLabelOnCell:(UITableViewCell*)cell
				 text:(NSString*)text
				frame:(CGRect)frame 
			  bgColor:(UIColor*)bgColor 
			textColor:(UIColor*)textColor 
		   fontOfSize:(UIFont*)fontOfSize 
		lineBreakMode:(UILineBreakMode)lineBreakMode 
		textAlignment:(UITextAlignment)textAlignment
{
	if(!bgColor) bgColor = [UIColor grayColor];
	if(!textColor) textColor = [UIColor blackColor];
	if(!fontOfSize) fontOfSize = [UIFont systemFontOfSize:16];
	if(!text) text = [NSString stringWithString:@"请输入Label内容"];
	
	UILabel* titleLabel = [[[UILabel alloc] initWithFrame:frame] autorelease];
	titleLabel.backgroundColor = bgColor;
	titleLabel.textColor = textColor;
	titleLabel.font = fontOfSize;
	titleLabel.lineBreakMode = lineBreakMode;
    titleLabel.numberOfLines = 50;
	titleLabel.textAlignment = textAlignment;
	titleLabel.text = text;
	[cell.contentView addSubview:titleLabel];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

@end
